package office.service.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.business.form.ContractForm;
import office.action.business.form.ContractProductDetailConditionForm;
import office.action.business.form.ContractProductDetailForm;
import office.action.business.wrapper.ContractProductDetailWrapper;
import office.bo.business.impl.ContractProductDetail;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.IContractProductDetailService;
import office.service.business.IContractService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class ContractProductDetailServiceImpl extends BaseBizServiceImpl implements IContractProductDetailService{
	private IContractService contractService;
	public void editContractProductDetail(ContractProductDetailForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			ContractProductDetail bo = (ContractProductDetail)domainProviderSetImpl.getContractProductDetailDP().newEmptyObj();
			bo =(ContractProductDetail) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑合同产品失败!", e);
		}
	}
	
	public Page getPageInfo(ContractProductDetailConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractProductDetailDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "ContractProductDetail.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findContractProductDetailByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<ContractProductDetailWrapper> findAllContractProductDetail(ContractProductDetailConditionForm condition,Page page)throws BusinessException{
		List<ContractProductDetailWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractProductDetailDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findContractProductDetailByIdAndName",condMap,new HashMap(),page);
			ContractProductDetailWrapper wrapper;
			for(int i=0;i<list.size();i++){
				ContractProductDetail bo = (ContractProductDetail)list.get(i);
				wrapper = new ContractProductDetailWrapper();
				if(bo.getContractId()!=null){
					ContractForm contract=contractService.getById(bo.getContractId());
					bo.setContractName(contract.getContractName());
				}
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取合同产品列表失败！", e);
		}
		return wrapperList;
	}

	public void delContractProductDetailById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractProductDetailDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除合同产品失败！", e);
		}
	}
	
	public ContractProductDetailForm getById(String id)throws BusinessException{
		ContractProductDetailForm form = new ContractProductDetailForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			ContractProductDetail bo = (ContractProductDetail)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getContractProductDetailDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取合同产品失败!", e);
		}
		return form;
	}
	/**
	 * 根据合同的ID获取合同对应的产品明细
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public List<ContractProductDetailWrapper> findAllContractProductDetailByContractId(ContractProductDetailConditionForm condition)throws BusinessException{
		List<ContractProductDetailWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractProductDetailDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getContractId())){
				condMap.put("contractId", condition.getContractId());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findContractProductDetailByContractId",condMap,new HashMap());
			ContractProductDetailWrapper wrapper;
			for(int i=0;i<list.size();i++){
				ContractProductDetail bo = (ContractProductDetail)list.get(i);
				wrapper = new ContractProductDetailWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据合同获取产品明细列表失败！", e);
		}
		return wrapperList;
	}
	
	public IContractService getContractService() {
		return contractService;
	}

	public void setContractService(IContractService contractService) {
		this.contractService = contractService;
	}
}
